<template>
    <div class="user-management-container">
      <el-card shadow="never" class="search-card">
        <el-form :inline="true" :model="searchForm" class="search-form">
          <el-form-item label="用户名">
            <el-input v-model="searchForm.account" placeholder="请输入用户名" clearable></el-input>
          </el-form-item>
          <el-form-item label="用户角色">
            <el-select v-model="searchForm.aopqx" placeholder="请选择角色" clearable>
              <el-option label="管理员" value="1"></el-option>
              <el-option label="普通用户" value="2"></el-option>
              <el-option label="访客" value="guest"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="searchForm.phone" placeholder="请输入手机号" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
  
      <el-card shadow="never" class="table-card">
        <div class="table-header">
          <el-button type="primary" @click="handleAdd">
            <i class="el-icon-plus"></i> 新增用户
          </el-button>
          <el-button :disabled="!selectedRows.length" @click="handleBatchDelete">
            <i class="el-icon-delete"></i> 批量删除
          </el-button>
        </div>
  
        <el-table
          :data="userList"
          border
          style="width: 100%"
          @selection-change="handleSelectionChange"
          v-loading="loading"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column label="序号" width="80">
            <template slot-scope="{ $index }">
              <!-- 计算当前页的起始序号，加上当前行在页内的索引 -->
              {{ (pagination.currentPage - 1) * pagination.pageSize + $index + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="account" label="账号"></el-table-column>
          <el-table-column prop="nickname" label="昵称"></el-table-column>
          <el-table-column prop="image" label="头像">
            <template slot-scope="scope">
              <!-- 保持图片标签，添加类名 -->
              <img :src="scope.row.image" class="table-avatar-full" alt="头像">
            </template>
          </el-table-column>
          <el-table-column prop="phone" label="手机/电话"></el-table-column>
          <el-table-column prop="email" label="邮箱"></el-table-column>
          <el-table-column prop="aopqx" label="角色">
            <template slot-scope="scope">
              <el-tag :type="scope.row.aopqx === 1 ? 'danger' : 'primary'">
                {{ scope.row.aopqx === 1 ? '管理员' : '普通用户' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="creTime" label="创建时间" width="180"></el-table-column>
          <el-table-column label="操作" width="180" fixed="right">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
  
        <div class="pagination-container">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pagination.currentPage"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="pagination.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pagination.total"
          ></el-pagination>
        </div>
      </el-card>
  
      <!-- 新增/编辑用户对话框 -->
      <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="50%">
        <el-form :model="userForm" :rules="rules" ref="userForm" label-width="100px">
          <el-form-item label="用户名" prop="account">
            <el-input v-model="userForm.account" placeholder="请输入账号"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" v-if="isAdd">
            <el-input v-model="userForm.password" placeholder="请输入密码" show-password></el-input>
          </el-form-item>
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="userForm.name" placeholder="请输入姓名"></el-input>
          </el-form-item>
          <el-form-item label="头像" prop="image">
            <el-input v-model="userForm.name" placeholder="请输入头像"></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="phone">
            <el-input v-model="userForm.phone" placeholder="请输入电话"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="userForm.email" placeholder="请输入邮箱"></el-input>
          </el-form-item>
       
          <el-form-item label="角色" prop="aopqx">
            <el-select v-model="userForm.aopqx" placeholder="请选择角色" style="width: 100%">
              <el-option label="管理员" value="1"></el-option>
              <el-option label="普通用户" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import axiosInstance from '@/utils/axios';
  export default {
    name: "UserManagement",
    data() {
      return {
        searchForm: {
          account: "",
          aopqx: "",
          phone: ""
        },
        userList: [],
        selectedRows: [],
        loading: false,
        pagination: {
          currentPage: 1,
          pageSize: 10,
          total: 0
        },
        dialogVisible: false,
        dialogTitle: "",
        isAdd: false,
        userForm: {
          id: "",
          account: "",
          password: "",
          name: "",
          email: "",
          phone: "",
          role: "user"
        },
        rules: {
          account: [
            { required: true, message: "请输入用户名", trigger: "blur" },
            { min: 3, max: 20, message: "长度在 3 到 20 个字符", trigger: "blur" }
          ],
          password: [
            { required: true, message: "请输入密码", trigger: "blur" },
            { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" }
          ],
          nickname: [
            { required: true, message: "请输入昵称", trigger: "blur" }
          ],
          image: [
            { required: true, message: "请上传照片", trigger: "blur" }
          ],
          email: [
            { required: true, message: "请输入邮箱", trigger: "blur" },
            { type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"] }
          ],
          phone: [
            { required: true, message: "请输入手机/电话", trigger: "blur" },
            { type: "phone", message: "请输入正确的手机/电话", trigger: ["blur", "change"] }
          ],
          aopqx: [
            { required: true, message: "请选择角色权限", trigger: "change" }
          ]
        }
      };
    },
    created() {
      this.fetchUserList();
    },
    methods: {
      fetchUserList() {
        this.loading = true;
        // 这里可以调用API获取用户列表数据
        axiosInstance.get('/admin/get',{
          params:{
          currentPage:this.pagination.currentPage,
          pageSize:this.pagination.pageSize,...this.searchForm
          }})
          .then(response => {
            console.log(response.data.data.listLimit);
            this.userList = response.data.data.listLimit; // 假设API返回的数据结构是 { data: [], total: 0 }
            // this.pagination.total = response.data.total; // 假设API返回的数据结构是 { data: [], total: 0 }
            this.loading = false; 
          })
      },
      handleSearch() {
        this.pagination.currentPage = 1;
        this.fetchUserList();
      },
      resetSearch() {
        this.searchForm = {
          account: "",
          role: ""
        };
        this.handleSearch();
      },
      handleSelectionChange(val) {
        this.selectedRows = val;
      },
      handleSizeChange(val) {
        this.pagination.pageSize = val;
        this.fetchUserList();
      },
      handleCurrentChange(val) {
        this.pagination.currentPage = val;
        this.fetchUserList();
      },
      handleAdd() {
        this.dialogTitle = "新增用户";
        this.isAdd = true;
        this.userForm = {
          id: "",
          account: "",
          password: "",
          name: "",
          email: "",
          phone: "",
          role: "user"
        };
        this.dialogVisible = true;
        this.$nextTick(() => {
          this.$refs.userForm.clearValidate();
        });
      },
      handleEdit(row) {
        this.dialogTitle = "编辑用户";
        this.isAdd = false;
        this.userForm = Object.assign({}, row);
        this.dialogVisible = true;
        this.$nextTick(() => {
          this.$refs.userForm.clearValidate();
        });
      },
      handleDelete(row) {
        this.$confirm(`确定删除用户 "${row.account}" 吗?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          // 这里添加删除逻辑
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.fetchUserList();
        });
      },
      handleBatchDelete() {
        const accounts = this.selectedRows.map(item => item.account).join(", ");
        this.$confirm(`确定删除选中的 ${this.selectedRows.length} 个用户吗?<br>${accounts}`, "提示", {
          dangerouslyUseHTMLString: true,
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          // 这里添加批量删除逻辑
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.fetchUserList();
        });
      },
      submitForm() {
        this.$refs.userForm.validate(valid => {
          if (valid) {
            // 这里添加提交逻辑
            this.$message({
              type: "success",
              message: this.isAdd ? "新增成功!" : "修改成功!"
            });
            this.dialogVisible = false;
            this.fetchUserList();
          } else {
            return false;
          }
        });
      }
    }
  };
  </script>
  
  <style lang="less" scoped>
  .user-management-container {
    padding: 20px;
  
    .search-card {
      margin-bottom: 20px;
  
      .search-form {
        display: flex;
        flex-wrap: wrap;
      }
    }
  
    .table-card {
      .table-header {
        margin-bottom: 15px;
        display: flex;
        align-items: center;
      }
    }
  
    .pagination-container {
      margin-top: 15px;
      text-align: right;
    }

    // 修改或添加头像样式
    .table-avatar-full {
      display: block;
      width: 100%;
      height: 100%;
      object-fit: cover; // 保持图片比例，填充整个容器，可能会裁剪图片
      object-position: center; // 图片居中显示
    }
  }
  </style>